package io.renren;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class GenericClassExample {
    public static void main(String[] args) {
        // 获取指定类的泛型类型参数
        Type genericType = getGenericType(MyClass.class);

        // 输出泛型类型参数信息
        if (genericType instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) genericType;
            Type[] typeArguments = parameterizedType.getActualTypeArguments();
            for (Type typeArgument : typeArguments) {
                System.out.println("泛型类型参数: " + typeArgument.getTypeName());
            }
        }
    }

    // 获取指定类的泛型类型参数
    public static Type getGenericType(Class<?> clazz) {
        Type genericType = clazz.getGenericSuperclass();
        while (!(genericType instanceof ParameterizedType)) {
            clazz = clazz.getSuperclass();
            genericType = clazz.getGenericSuperclass();
        }
        return genericType;
    }

    // 示例类，包含泛型类型参数
    public static class MyClass<T, U> {
        // 类的实现
    }
}
